
var vm = new Vue({
  el: "#app",
  data: {
    isagreement: false,//是否同意用户协议
    leixing_text:"",//类型显示
    end_time_val:"",
    shop_list: [],
    shop_name: '',
    pic: null,//封面id
    type: "",//活动分类
    type_diy: "",//自定义分类
    cost: "",//活动价
    cost_type: 0,//	活动价类型
    stop_time: null,//	截止时间
    desc: null,//详情部分
    time: "",
  },
  mounted: function () {
    this.init();
  },
  methods: {
    init: function () {
      if ($.cookie("activity") &&$.cookie("activity")!="null") {
        console.log("获得cookie");
        var cookie_ = JSON.parse($.cookie("activity"));
        if($.cookie("tiyu_leixing")&&$.cookie("tiyu_leixing")!="null"){
          var leixing_cookie = JSON.parse($.cookie("tiyu_leixing"));
          console.log(leixing_cookie);
          this.type= leixing_cookie.type;
           this.type_diy=  JSON.stringify(leixing_cookie.type_diy);
           this.leixing_text = leixing_cookie.text[0];
        }
        if($.cookie("nz_list") &&$.cookie("nz_list")!="null"){
          
          var nz_list = $.cookie("nz_list");
          console.log(nz_list);
          this.alert_time = nz_list;
          this.nz_text= JSON.parse(nz_list).length;
          console.log(this.nz_text);
          this.alert_time= nz_list;
        }
        console.log(cookie_);
        this.pic = cookie_.pic;
        this.pic_src = cookie_.pic_src;
        this.is_c_yuan = cookie_.is_c_yuan;
        this.isagreement = cookie_.isagreement;
        this.shop_list = cookie_.shop_list;
        this.shop_name = cookie_.shop_name;
        // this.type = cookie_.type;
        // this.type_diy = cookie_.type_diy;
        this.cost = cookie_.cost;
        this.cost_type = cookie_.cost_type;
        this.stop_time = cookie_.stop_time;
        // this.alert_time = cookie_.alert_time;
        this.desc = cookie_.desc;
      }
    },
    //设置cookie
    set_cookie: function () {
      var cookie_ = {};
      cookie_.pic = this.pic;
      cookie_.pic_src = this.pic_src;
      cookie_.is_c_yuan = this.is_c_yuan;
      cookie_.isagreement = this.isagreement;
      cookie_.shop_list = this.shop_list;
      cookie_.shop_name = this.shop_name;
      // cookie_.type = this.type;
      // cookie_.type_diy = this.type_diy;
      cookie_.cost = this.cost;
      cookie_.cost_type = this.cost_type;
      cookie_.stop_time = this.stop_time;
      // cookie_.alert_time = this.alert_time;
      cookie_.desc = this.desc;
      $.cookie("activity", JSON.stringify(cookie_));
      console.log("logset");
    },
    //标题图片点击
    add_click: function () {
      console.log(1234563);
      // document.getElementById("add_input").click();
      console.log($("#add_input"));
      $("#add_input").click();
    },


    //添加内容
    add: function () {
      this.shop_list.push({ text: '', img: [] });
    },
    //添加图片
    add_img: function (e, index) {
      console.log(e);
      console.log(e.currentTarget);
      var a = $(e.currentTarget).siblings("input");
      a.click();
      console.log(a);
      console.log(index);
    },
    // 内容部分图片上传
    update_img_content: function (e, index) {
      var dom = e.currentTarget;
      console.log(dom);
      var file = dom.files[0];
      //创建formdata对象
      var formdata = new FormData();
      formdata.append("file", file);
      formdata.append("dir", "images");
      // this.pic_src = window.URL.createObjectURL(file);
      // $(e.currentTarget).append();
      // 上传
      $.ajax({
        url: app.webUrl + "/index/plugin/execute/_plugin/WxSdk/_controller/Api/_action/file",
        type: "POST",
        data: formdata,
        processData: false,// 告诉jQuery不要去处理发送的数据
        contentType: false,
        success: function (ret) {
          var data = JSON.parse(ret);
          console.log(data);
          if (data.code == '1') {
            console.log(data.id + data.url);
            var img = { id: data.id, src: data.url };
            console.log(img);

            vm.shop_list[index].img.push(img);
            vm.set_cookie();
            // vm.pid = ret.id;
            // vm.send_img(data.url);
          }
        },
      });
    },
    //改变选择协议
    change_agreement: function () {
      this.isagreement = !this.isagreement;
    },
    //发布活动
    publish: function () {
      // setTimeout(function(){
      //   window.location.href = '/view/publish/fbhdcg.html'
      // },2000)
      if (this.shop_name == "") {
        vant.Toast({ message: "请设置活动名称");
        return;
      }
      console.log(this.pic);
      
      if (this.pic == null) {
        vant.Toast({ message: "请设置标题图片");
        return;
      }
      
      if (this.cost ==""||Number(this.cost)<0) {
        vant.Toast({ message: "请设置正确的活动价");
        return;
      }
      // this.set_cookie();
      //获取详情数据
      var list = this.shop_list.map(function (item) {
        var obj = {}
        obj.desc = item.text;
        var imgidArr = item.img.map(function (tab) {
          return tab.id
        });
        var str = imgidArr.join(',');
        obj.pics = str;
        return obj;
      });
      this.desc = JSON.stringify(list);
      console.log(list);
      //截止时间
      var date_stop = $('#date-group1-4').val();
      var stopTime = new Date(date_stop).getTime();
      var date = new Date().getTime();
      if (date_stop == "") {
        vant.Toast({ message: "请选择截止时间");
        return;
      }
      if (this.leixing_text == "") {
        vant.Toast({ message: "请选择活动类型");
        return;
      }
      if (date > stopTime) {
        vant.Toast({ message: "截止时间应该晚于当前时间");
        return;
      }
      if (this.isagreement != true) {
        vant.Toast({ message: "请同意用户协议");
        return;
      }
      this.stop_time = (stopTime / 1000);
      console.log(this.stop_time);
      var data = {
        name:vm.shop_name,
        pic:vm.pic,
        type:vm.type,
        type_diy:vm.type_diy,
        cost:vm.cost,
        cost_type:vm.cost_type,
        stop_time:vm.stop_time,
        alert_time:vm.alert_time,
        desc:vm.desc
      }
      if(vm.alert_time==""){
        data = {
          name:vm.shop_name,
          pic:vm.pic,
          type:vm.type,
          type_diy:vm.type_diy, 
          cost:vm.cost,
          cost_type:vm.cost_type,
          stop_time:vm.stop_time,
          desc:vm.desc
        }
      }
      console.log(data);
      
      $.ajax({
        url: app.webUrl + "/view/api_activity/add",
        type: "POST",
        data: data,
        success: function (ret) {
          console.log(ret);          
          if (ret.code == '1') {
            $.cookie("activity",null)
            $.cookie("tiyu_leixing",null)
            $.cookie("nz_list",null)
          vant.Toast({ message: "活动发布成功");
          setTimeout(function(){
            window.location.href = '/view/publish/fbhdcg.html?&activityId='+ret.data.id;
          },2000)
          }
        },
      });
    },
    back:function(){
      $.cookie("activity",null)  
      $.cookie("tiyu_leixing",null)
      $.cookie("nz_list",null)
      window.location.href="/view/index/index.html";
    },

  },

  //
})

window.onload=function() {
  new Rolldate({
    el: '#date-group1-4',
    format: 'YYYY-MM-DD hh:mm',
    beginYear: 2000,
    endYear: 2100
  }) 
}


$('.set_tag .right').click(function() {
  window.location.href = '/view/publish/leixing.html'
})
$('.set_price .right').click(function() {
  $('.price_box').css('display','block')
  $('.price_box .cont').css('display','block')
})


function guanbi() {
  $('.price_box').css('display','none')
  $('.price_box .cont').css('display','none')
}

function quedingjg() {
  $('.price_box').css('display','none')
  $('.price_box .cont').css('display','none')
}



// // 点击加号，在后方生成组件
// const detail = `
// <div class="detail_cont">
//   <textarea placeholder="请编写商品详情信息..."></textarea>
//   <ul>
//     <li>
//       <img src="/public/img/chong.png" alt="">
//     </li>
//     <img src="/public/img/add.png" alt="">
//     <input type="file" multiple capture='camera' accept='image/*' style="display: none;">
//   </ul>
// </div>
// `
// $('.detail .title .add').click(function () {
//   $(detail).appendTo('.detail');
// })

